package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsBankVou;
import com.glsc.ngateway.common.base.repo.oracle.xinyizj.FsBankVouRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class FsBankVouService {


    @Resource
    private FsBankVouRepository fsBankVouRepository;

    /**
     * 计算银证转账金额
     *
     * @param payBankAcc
     * @return
     */
    public BigDecimal calYzAmount(String payBankAcc) {
        BigDecimal res = BigDecimal.ZERO;
        List<FsBankVou> payList = listByPayBankAcc(payBankAcc);
        List<FsBankVou> recList = listByRecBankAcc(payBankAcc);

        if (CollectionUtils.isNotEmpty(payList)) {
            for (FsBankVou fsBankVou : payList) {
                if (Objects.nonNull(fsBankVou.getOccurBal())) {
                    res = res.add(fsBankVou.getOccurBal());
                }
            }
        }

        if (CollectionUtils.isNotEmpty(recList)) {
            for (FsBankVou fsBankVou : recList) {
                res = res.add(fsBankVou.getOccurBal());
            }
        }

        return res;
    }


    /**
     * 根据付款账号查询所有付款流水信息
     *
     * @param payBankAcc
     * @return
     */
    public List<FsBankVou> listByPayBankAcc(String payBankAcc) {
        List<FsBankVou> list = fsBankVouRepository.findAll(Example.of(FsBankVou.builder().payBankAcc(payBankAcc).build()));
        return list;
    }

    /**
     * 根据收款账号查询所有付款流水信息
     *
     * @param recBankAcc
     * @return
     */
    public List<FsBankVou> listByRecBankAcc(String recBankAcc) {
        List<FsBankVou> list = fsBankVouRepository.findAll(Example.of(FsBankVou.builder().recBankAcc(recBankAcc).build()));
        return list;
    }

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<FsBankVou> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "bankAcc";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<FsBankVou> page = fsBankVouRepository.findAll(SpecificationUtil.buildSpecification(param, FsBankVou.class), pageable);

        return page;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<FsBankVou> listAll() {
        List<FsBankVou> list = fsBankVouRepository.findAll(Example.of(FsBankVou.builder().build()));
        return list;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public FsBankVou findById(String id) {
        Optional<FsBankVou> optional = fsBankVouRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }


}
